#include "stdafx.h"

#include "FAlpResBtn.h"
#include "Control/Container.h"


#define MAKE_RECT( X,Y,W,H )  X,Y,(X+W),(Y+H)


CFAlpResBtn::CFAlpResBtn(void)
	:CBaseICtrl()
{
	Init();
}

CFAlpResBtn::CFAlpResBtn(UINT nID)
	:CBaseICtrl( nID )
{
	Init();
}

CFAlpResBtn::CFAlpResBtn(wstring strCaption,UINT nID)
	:CBaseICtrl( strCaption,nID )
{
	Init();
}

CFAlpResBtn::CFAlpResBtn(DWORD dwStyle,wstring strCaption, const CRect &rc, UINT nID )
	:CBaseICtrl( dwStyle, strCaption, rc,nID )
{
	Init();
}

CFAlpResBtn::CFAlpResBtn(DWORD dwStyle, wstring strCaption, int x, int y, int cx, int cy, UINT nID )
	:CBaseICtrl( dwStyle, strCaption, x, y, cx, cy,  nID )
{
	Init();
}

CFAlpResBtn::~CFAlpResBtn(void)
{
	for ( int i=0;i<STBTN_ALL;i++ )
	{
		if( NULL!=m_pBtnAlpRes[i] )
		{
			delete m_pBtnAlpRes[i];
			m_pBtnAlpRes[i] = NULL;
		}
	}
}

void CFAlpResBtn::Init()
{
	m_nBtnState = 0;
	for ( int i=0;i<STBTN_ALL;i++ )
	{
		m_pBtnAlpRes[i] = NULL;
	}
	m_strBtnWav =  CUtil::GetCurrentDirectory() + L"\\ABtn.wav";
}

void	CFAlpResBtn::SetClickSound( LPCTSTR pStrSound )
{
	m_strBtnWav =  pStrSound;
}

// conctrol only have one image
BOOL	CFAlpResBtn::LoadRes( wstring strRes,CMyUnCompress* pPack )
{
	return LoadStateRes(  strRes, STBTN_NOR, pPack );
}

//if contrl only have one img,this function just like LoadRes,here the same
BOOL	CFAlpResBtn::LoadStateRes( wstring strRes,int nState,CMyUnCompress* pPack )
{
	BOOL br = FALSE;
	if ( nState<STBTN_ALL && nState>=0 )
	{
		if ( m_pBtnAlpRes[nState]==NULL )	
			m_pBtnAlpRes[nState] = new CFAlphaRes();
		br = m_pBtnAlpRes[nState]->LoadResData( strRes,pPack );
	}
	return br;
}

BOOL	CFAlpResBtn::CloneStateBtn( int nSrcSt,int dst_State )
{
	if ( nSrcSt>=STBTN_ALL || nSrcSt<0 && dst_State>=STBTN_ALL && dst_State<0 )
		return FALSE;
	if ( m_pBtnAlpRes[nSrcSt]==NULL )
		return FALSE;

	if( NULL!=m_pBtnAlpRes[dst_State] )
	{
		delete m_pBtnAlpRes[dst_State];
		m_pBtnAlpRes[dst_State] = NULL;
	}
	m_pBtnAlpRes[dst_State] = new CFAlphaRes( *(m_pBtnAlpRes[nSrcSt]) );
	return TRUE;
}

void	CFAlpResBtn::RSetBtnState( int nState )
{
	if ( nState<STBTN_ALL && nState>=0 )
		m_nBtnState = nState;
}

void  CFAlpResBtn::Paint(CDC* pDC)
{
	return;
}

void  CFAlpResBtn::ImprovePaint( HANDLE hDB ,HDC hdc )
{
	//if ( ST_UNKNOW==m_nBtnState )return ;

	if( m_rcControl.IsRectEmpty() || !IsVisible() )
	{
		return ;
	}

	if ( m_nBtnState<STBTN_ALL && m_nBtnState>=STBTN_NOR )
	{
		if ( m_pBtnAlpRes[m_nBtnState]!=NULL )
		{
			FASTBLTALPHABPP( m_pBtnAlpRes[m_nBtnState], hDB,m_rcControl.TopLeft(),GetConBKSize() );
		}
	}

	if ( hdc!=NULL && (STBTN_NOR==m_nBtnState||STBTN_ACT==m_nBtnState ) )
	{
		PaintCaption( hdc );
	}
}

void  CFAlpResBtn::ImproveStatePaint( HANDLE hDB ,BTN_STATE state )
{
	if ( state<STBTN_ALL && state>=STBTN_NOR )
	{
		if ( m_pBtnAlpRes[state]!=NULL )
		{
			FASTBLTALPHABPP( m_pBtnAlpRes[state],  hDB,m_rcControl.TopLeft(),GetConBKSize() );
		}
	}
}

void  CFAlpResBtn::PaintCaption( HDC hdcTxt )
{
	SetBkMode( hdcTxt, TRANSPARENT );
	if ( GetState()==STATE_PUSHED )
	{
		SetTextColor( hdcTxt,m_clrTxtActive );
	}
	else
	{
		SetTextColor( hdcTxt,m_clrTxtNormal );
	}

	CRect rcTxt( m_rcControl );
	rcTxt.top += m_szTextOffset.cy;

	if(m_dwAlign&DT_RIGHT)
		rcTxt.right -= m_szTextOffset.cx;
	else
		rcTxt.left += m_szTextOffset.cx;
	

	HFONT hOldFont = NULL;
	if ( m_pFont!=NULL )
		hOldFont = (HFONT)SelectObject( hdcTxt, m_pFont->m_hFont );
	
	DrawText( hdcTxt, m_strCaption.c_str(),m_strCaption.length(),rcTxt,m_dwAlign );
	
	if ( hOldFont!=NULL )
		SelectObject( hdcTxt,hOldFont );
	
}

void  CFAlpResBtn::Click(DWORD dwClick, POINT pt)
{
	if ( !IsEnabled() || !IsVisible() || m_rcControl.IsRectEmpty() )
	{
		return;
	}

	if ( dwClick&CLICK_DOWN )
	{
		PlaySound( m_strBtnWav.c_str(), NULL, SND_FILENAME|SND_ASYNC|SND_NODEFAULT );
		SetState( STATE_PUSHED ); 
		RSetBtnState( STBTN_ACT );
	}
	else if ( dwClick&CLICK_UP )
	{
		SetState( STATE_NORMAL ); 
		RSetBtnState( STBTN_NOR );
	}
	else
	{
		RSetBtnState( STBTN_NOR );	
	}

	if ( m_pContainer!=NULL && IsWindow( m_pContainer->m_hWnd) )
	{
		//CClientDC ClientDC( m_pContainer->m_hWnd );
		//CContainer *pImpCon = (CImproveCon *)m_pContainer;
		//pImpCon->Paint( &ClientDC,m_rcControl );

		InvalidateRect( m_pContainer->m_hWnd,m_rcControl,FALSE );
		UpdateWindow( m_pContainer->m_hWnd );
	}

	if ( dwClick&CLICK_UP && GetRect().PtInRect( pt ) )
	{
		CUtil::PlayBeepSound();
		::SendMessage( m_pContainer->m_hWnd,WM_COMMAND,GetID(),NULL);
	}
}

